package com.xing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.LoginDTO;
import com.xing.DTO.UserDTO;
import com.xing.dao.UserDao;
import com.xing.entity.UserDetail;
import com.xing.entity.UserEntity;
import com.xing.service.UserService;
import com.xing.utils.JwtUtils;
import com.xing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/7/16 13:46
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private UserDao dao;


    @Override
    public UserDetail getUserDetailByUsername(String username) {
        QueryWrapper<UserEntity> query = new QueryWrapper<>();
        query.eq("username", username);
        UserEntity userEntity = dao.selectOne(query);
        UserDetail userDetail = new UserDetail();
        UserDTO dto = new UserDTO();
        dto.setUsername(userEntity.getUsername());
        dto.setPassword(userEntity.getPassword());
        userDetail.setUser(dto);
        return userDetail;
    }

    public Result Login(LoginDTO dto) {
        QueryWrapper<UserEntity> query = new QueryWrapper<>();
        query.eq("username", dto.getUsername());
        UserEntity userEntity = dao.selectOne(query);
        if (userEntity != null) {
            if (new BCryptPasswordEncoder().matches(dto.getPassword(),userEntity.getPassword())) {
                String token = JwtUtils.createToken(userEntity);
                return new Result().ok(token);
            } else {
                return new Result().err(400, "账号或密码错误");
            }
        }
        return new Result().err(501, "账号不存在");
    }

    @Override
    public Result Register(LoginDTO dto) {
        QueryWrapper<UserEntity> query = new QueryWrapper<>();
        query.eq("username", dto.getUsername());
        boolean exists = dao.exists(query);
        if (exists) {
            return new Result().err(501, "账号已被注册");
        } else {
            UserEntity userEntity = new UserEntity();
            userEntity.setUsername(dto.getUsername());
            userEntity.setPassword(new BCryptPasswordEncoder().encode(dto.getPassword()));
            userEntity.setEmail(dto.getEmail());
            userEntity.setCreated(new Date());
            userEntity.setLastModified(new Date());
            dao.insert(userEntity);
        }
        return new Result().ok("注册成功");
    }

    @Override
    public Result GetMe() {
        UserDTO principal = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = principal.getUserId();
        UserEntity entity = getById(userId);
        if (ObjectUtil.isEmpty(entity)) {
            return new Result().err(601, "身份信息异常");
        }
        return new Result().ok(entity);
    }


}
